package com.shouhuobao.take.home;

import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iseastar.guojiang.BaseFragment;
import com.iseastar.guojiang.app.AppCache;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.iseastar.guojiang.model.UserBean;
import com.shouhuobao.station.R;
import com.shouhuobao.take.verify.MyStationInfoActivity;
import com.shouhuobao.take.verify.RegVerifyActivity1;
import com.shouhuobao.take.verify.RegVerifyRankActivity;

/**
 * Created by joker on 2017/9/15.
 * 代收首页和店小三首页基类
 */
public class BaseHomeFragment extends BaseFragment {

    protected int type;//标记是代收的home还是寄件的home  0 代收  1寄件

    @Override
    protected void findViewById() {
        super.findViewById();
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            //根据手机号查询驿站状态信息
            case MsgID.take_getuser_info:
                final ReqResult<UserBean> reqResult = JSON.parser(msg.obj, UserBean.class);
                getActivityContext().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(reqResult)) {
                            UserBean user = reqResult.getResult();
                            if (user != null) {
                                if (isEmpty(user.getPhone())) {
                                    user.setPhone(AppCache.getUser().getPhone());
                                }
                                AppCache.setUser(user);
                                updateTitleView(reqResult.getStatus(), reqResult.getMessage());
                            }
                        } else {
                            updateTitleView(reqResult.getStatus(), reqResult.getMessage());
                        }
                    }
                });
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    /**
     * 只有审核通过才可以使用，其他情况所有功能都不能使用
     */
    public void updateTitleView(int status, String message) {
        try {
            LinearLayout layout_LL = (LinearLayout) findViewById(R.id.home_layout);
            LinearLayout tipsLL = (LinearLayout) findViewById(R.id.tipsLL);
            TextView tipsTV = (TextView) findViewById(R.id.tipsTV);
            TextView tipRightTV = (TextView) findViewById(R.id.tip_right_tv);
            LinearLayout takeTipsLL = (LinearLayout) findViewById(R.id.take_tips_LL);
            if (status == 3002) {//驿站未注册  跳转到选择驿站申请界面
                tipsLL.setVisibility(View.VISIBLE);
                tipsTV.setText(message);
                tipRightTV.setText("点击进入");
                tipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(RegVerifyRankActivity.class);
                    }
                });
                takeTipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(RegVerifyRankActivity.class);
                    }
                });
                layout_LL.setFocusable(false);
            } else if (status == 3003) {//驿站正在审核  跳转到审核状态详情
                tipsLL.setVisibility(View.VISIBLE);
                tipsTV.setText(message);
                tipRightTV.setText("查看详情");
                tipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivityContext(), MyStationInfoActivity.class);
                        intent.putExtra("type", 1);
                        startActivity(intent);
                    }
                });
                takeTipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getActivityContext(), MyStationInfoActivity.class);
                        intent.putExtra("type", 1);
                        startActivity(intent);
                    }
                });
            } else if (status == 3004) {//3004 审核失败 跳转到填写资料界面
                tipsLL.setVisibility(View.VISIBLE);
                tipsTV.setText(message);
                tipRightTV.setText("立即修改");
                tipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(RegVerifyActivity1.class);
                    }
                });
                takeTipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(RegVerifyActivity1.class);
                    }
                });
            } else if (status == 3005) {//驿站被冻结，不能继续使用  不作任何点击事件
                tipsLL.setVisibility(View.VISIBLE);
                tipsTV.setText(message);
                tipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                    }
                });
                takeTipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            } else if (status == 3007) {//驿站已经支付，未填写资料  跳转到填写资料界面
                tipsLL.setVisibility(View.VISIBLE);
                tipsTV.setText(message);
                tipRightTV.setText("立即填写");
                tipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(RegVerifyActivity1.class);
                    }
                });
                takeTipsLL.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(RegVerifyActivity1.class);
                    }
                });
            } else {
                tipsLL.setVisibility(View.GONE);
                tipsTV.setText(message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
